package com.common.print;

/**
 * @ClassName SynchronicedDemo
 * @Description Synchronized + wait + notifyAll 实现循环打印ABC
 * @Author admin
 * @Date 2021/4/27 16:22
 * @Version 1.0
 **/
public class SynchronizedDemo {

    private static int num = 1;

    /**
     * @Description: 注意wait()和notifyAll()的使用：
     * 1.被唤醒的线程不是重新执行run方法，而是继上次阻塞的地方重新执行，也就是wait方法之后；所以，在使用wait时，判断条件要使用while而不是if
     * 2.wait和notifyAll一定要在同步代码块中使用
     * @Author: liuhuanjin
     * @Date: 2021/4/28 10:58
     * @param args:
     * @return: void
     **/
    public static void main(String[] args) {

        // 构建的锁对象 使用Object o = new Object();
        Object print = new Object();
            Thread threadA = new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int j = 0; j < 2; j++){
                        synchronized (print){
                            while (num != 1){
                                try {
                                    print.wait();
                                } catch (InterruptedException e){
                                    e.printStackTrace();
                                }
                            }
                            System.out.println("AAA");
                            num = 2;
                            print.notifyAll();
                        }
                    }
                }
            });

            Thread threadB = new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int j = 0; j < 2; j++){
                        synchronized (print){
                            while (num != 2){
                                try {
                                    print.wait();
                                } catch (InterruptedException e){
                                    e.printStackTrace();
                                }
                            }
                            System.out.println("BBB");
                            num = 3;
                            print.notifyAll();
                        }
                    }
                }
            });

            Thread threadC = new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int j = 0; j < 2; j++){
                        synchronized (print){
                            while (num != 3){
                                try {
                                    print.wait();
                                } catch (InterruptedException e){
                                    e.printStackTrace();
                                }
                            }
                            System.out.println("CCC");
                            num = 1;
                            print.notifyAll();
                        }
                    }
                }
            });


            threadA.start();
            threadB.start();
            threadC.start();
        }

}
